package netwrix.selenium;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.WebDriver;

public class SolutionJenkins {
	
private WebDriver driver;

	@Before
	public void setUp(){
	//System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32_2.9\\chromedriver.exe");
	//System.setProperty("webdriver.chrome.driver", "C:/operadriver_win32/operadriver.exe");
    driver = new FirefoxDriver();
	driver.get("http://www.netwrix.com");
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}
		
	@After
	public void tearDown(){
	driver.quit();
	}
	
	@Test
	public void testExamples(){
	loadHomePage();
	driver.findElement(By.linkText("Solutions")).click();
	driver.findElement(By.linkText("Netwrix Auditor for Active Directory")).click();
	driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=naad')]")).click();
	System.out.println(driver.getTitle());
	System.out.println(driver.getCurrentUrl());
	driver.findElement(By.linkText("Request Quote")).click();
	driver.findElement(By.xpath("(//a[contains(@href, './ad_audit.html')])[2]")).click();
	askAQuestion();
	driver.findElement(By.linkText("Netwrix Auditor for Active Directory")).click();
	oneToOneAutoFill();
//	driver.findElement(By.xpath("(//a[contains(@href, './ad_audit.html')])[2]")).click();
//	onlineTestDrive();
	
	driver.findElement(By.linkText("Solutions")).click();
	driver.findElement(By.linkText("Netwrix Auditor for Exchange")).click();
	driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=naex')]")).click();
	System.out.println(driver.getTitle());
	System.out.println(driver.getCurrentUrl());
	driver.findElement(By.linkText("Request Quote")).click();
	driver.findElement(By.xpath("(//a[contains(@href, './exchange_reporting.html')])[2]")).click();
	askAQuestion();
	driver.findElement(By.linkText("Netwrix Auditor for Exchange")).click();
	oneToOneAutoFill();
	//driver.findElement(By.xpath("(//a[contains(@href, './exchange_reporting.html')])[2]")).click();
	//onlineTestDrive();
	
	driver.findElement(By.linkText("Solutions")).click();
	driver.findElement(By.linkText("Netwrix Auditor for File Servers")).click();
	driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=nafs')]")).click();
	System.out.println(driver.getTitle());
	System.out.println(driver.getCurrentUrl());
	driver.findElement(By.linkText("Request Quote")).click();
	driver.findElement(By.xpath("(//a[contains(@href, './file_server_auditing.html')])[2]")).click();
	askAQuestion();
	driver.findElement(By.linkText("Netwrix Auditor for File Servers")).click();
	oneToOneAutoFill();
	//driver.findElement(By.xpath("(//a[contains(@href, './file_server_auditing.html')])[2]")).click();
	//onlineTestDrive();
	
	driver.findElement(By.linkText("Solutions")).click();
	driver.findElement(By.linkText("Netwrix Auditor for SharePoint")).click();
	driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=nasp')]")).click();
	System.out.println(driver.getTitle());
	System.out.println(driver.getCurrentUrl());
	driver.findElement(By.linkText("Request Quote")).click();
	driver.findElement(By.xpath("(//a[contains(@href, './sharepoint_reporting.html')])[2]")).click();
	askAQuestion();
	driver.findElement(By.linkText("Netwrix Auditor for SharePoint")).click();
	oneToOneAutoFill();
	//driver.findElement(By.xpath("(//a[contains(@href, './sharepoint_reporting.html')])[2]")).click();
	//onlineTestDrive();
	
	driver.findElement(By.linkText("Solutions")).click();
	driver.findElement(By.linkText("Netwrix Auditor for SQL Server")).click();
	driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=nasql')]")).click();
	System.out.println(driver.getTitle());
	System.out.println(driver.getCurrentUrl());
	driver.findElement(By.linkText("Request Quote")).click();
	driver.findElement(By.xpath("(//a[contains(@href, './sql_server_audit_reporting.html')])[2]")).click();
	askAQuestion();
	driver.findElement(By.linkText("Netwrix Auditor for SQL Server")).click();
	oneToOneAutoFill();
	//driver.findElement(By.xpath("(//a[contains(@href, './sql_server_audit_reporting.html')])[2]")).click();
	//onlineTestDrive();
	
	driver.findElement(By.linkText("Solutions")).click();
	driver.findElement(By.linkText("Netwrix Auditor for VMware")).click();
	driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=navm')]")).click();
	System.out.println(driver.getTitle());
	System.out.println(driver.getCurrentUrl());
	driver.findElement(By.linkText("Request Quote")).click();
	driver.findElement(By.xpath("(//a[contains(@href, './vmware_reporting.html')])[2]")).click();
	askAQuestion();
	driver.findElement(By.linkText("Netwrix Auditor for VMware")).click();
	oneToOneAutoFill();
	//driver.findElement(By.xpath("(//a[contains(@href, './vmware_reporting.html')])[2]")).click();
	//onlineTestDrive();
	
	driver.findElement(By.linkText("Solutions")).click();
	driver.findElement(By.linkText("Netwrix Auditor for Windows Server")).click();
	driver.findElement(By.xpath("//a[contains(@href, 'requeste.html?product=naws')]")).click();
	System.out.println(driver.getTitle());
	System.out.println(driver.getCurrentUrl());
	driver.findElement(By.linkText("Request Quote")).click();
	driver.findElement(By.xpath("(//a[contains(@href, './windows_server_audit.html')])[2]")).click();
	askAQuestion();
	driver.findElement(By.linkText("Netwrix Auditor for Windows Server")).click();
	oneToOneAutoFill();
	//driver.findElement(By.xpath("(//a[contains(@href, './windows_server_audit.html')])[2]")).click();
	//onlineTestDrive();
	
}

	private void onlineTestDrive() {
		driver.findElement(By.linkText("Online TestDrive")).click();
		driver.findElement(By.id("requesttd_btn")).click();
		String winHandleBefore = driver.getWindowHandle(); 
for (String winHandle : driver.getWindowHandles()) {
		driver.switchTo().window(winHandle);
}
		driver.close();
		driver.switchTo().window(winHandleBefore);
		quizTestDrive();
	}

	private void askAQuestion() {
		driver.findElement(By.linkText("Ask Question")).click();
		driver.findElement(By.id("AskQuestionForm_questionText")).clear();
		driver.findElement(By.id("AskQuestionForm_questionText")).sendKeys("Selenium test");
		driver.findElement(By.name("yt0")).click();
		driver.findElement(By.name("yt0")).click();
		new WebDriverWait(driver, 30).until(ExpectedConditions.textToBePresentInElementLocated(By.id("mbody"), "We will try"));
	}

	private void oneToOneAutoFill() {
		driver.findElement(By.cssSelector("#yw3 > li > a")).click();
		driver.findElement(By.id("OneToOneForm_firstName")).clear();
		driver.findElement(By.id("OneToOneForm_firstName")).sendKeys("Erik");
		driver.findElement(By.id("OneToOneForm_lastName")).clear();
		driver.findElement(By.id("OneToOneForm_lastName")).sendKeys("Borg");
		new Select(driver.findElement(By.id("OneToOneForm_position"))).selectByVisibleText("IT Director");
		driver.findElement(By.id("OneToOneForm_companyName")).clear();
		driver.findElement(By.id("OneToOneForm_companyName")).sendKeys("Netwrix");
		driver.findElement(By.id("OneToOneForm_email")).clear();
		driver.findElement(By.id("OneToOneForm_email")).sendKeys("erik.borg@netwrix.com");
		new Select(driver.findElement(By.id("OneToOneForm_nwf_country"))).selectByVisibleText("United States");
		new Select(driver.findElement(By.id("OneToOne_nwf_state"))).selectByVisibleText("California - Northern");
		driver.findElement(By.id("OneToOneForm_dateTimePref")).clear();
		driver.findElement(By.id("OneToOneForm_dateTimePref")).sendKeys("Selenium test");
		driver.findElement(By.name("yt0")).click();
		driver.findElement(By.name("yt0")).click();
		new WebDriverWait(driver, 30).until(ExpectedConditions.textToBePresentInElementLocated(By.id("mbody"), "thank you for requesting"));
	
	}

	private void loadHomePage() {
		WebElement signinButton = driver.findElement(By.linkText("Sign In"));
		signinButton.click();
		driver.findElement(By.linkText("Already registered?")).click();
		driver.findElement(By.id("LoginForm_username")).clear();
		driver.findElement(By.id("LoginForm_username")).sendKeys("erik.borg@netwrix.com");
		driver.findElement(By.id("LoginForm_password")).clear();
		driver.findElement(By.id("LoginForm_password")).sendKeys("123456");
		driver.findElement(By.name("yt1")).click();
	}

	private void quizTestDrive() {
		driver.findElement(By.id("answer39929X35X391Out")).click();
		driver.findElement(By.id("answer39929X35X395Out")).click();
		driver.findElement(By.id("answer39929X35X406Out")).click();
		driver.findElement(By.id("answer39929X35X399")).clear();
		driver.findElement(By.id("answer39929X35X399")).sendKeys("Selenium test");
		driver.findElement(By.id("movesubmitbtn")).click();
	}
}

